/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.moedik.controller;

import com.moedik.model.ConfigModel;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author Sammy
 */
public class SettingsController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        if (request.getParameter("action").equals("edit")) {
            if (request.getMethod().equals("POST")) {
                String company = request.getParameter("company");
                
                ConfigModel configModel = new ConfigModel();
                configModel.setCompanyName(company);
                
                company = configModel.getCompanyName();
                request.getSession().setAttribute("companyName", company);
                
                request.setAttribute("title", "Settings");
                request.setAttribute("company", company);
                request.getRequestDispatcher("settings/settings.jsp").forward(request, response);
            } else {
                ConfigModel configModel = new ConfigModel();
                String company = configModel.getCompanyName();
                
                request.setAttribute("title", "Settings");
                request.setAttribute("company", company);
                request.getRequestDispatcher("settings/settings.jsp").forward(request, response);
            }
        } else if (request.getParameter("action").equals("upload")) {
            if (request.getMethod().equals("POST")) {
                // get your absolute path
                String uploadTo = getServletContext().getRealPath("/") + "assets/img/";
                
                try {
                    
                    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

                    // no multipart form
                    if (!isMultipart) {
                        out.println("<html>");
                        out.println("<head>");
                        out.println("<title>Servlet UploadServlet</title>");
                        out.println("</head>");
                        out.println("<body>");
                        out.println("<h1>No Multipart Files</h1>");
                        out.println("</body>");
                        out.println("</html>");
                    } // multipart form
                    else {
                        // Create a new file upload handler
                        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());

                        // parse requests
                        List<FileItem> fileItems = upload.parseRequest(request);

                        // Process the uploaded items
                        for (FileItem fileItem : fileItems) {
                            // a regular form field
                            if (fileItem.isFormField()) {
                            } // upload field
                            else {
                                String fileName = "logo.png";
                                File fileTo = new File(uploadTo + fileName);
                                fileItem.write(fileTo);
                                response.sendRedirect("settings?action=edit");
                            }
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    out.close();
                }
            } else {
                request.setAttribute("title", "Settings");
                request.getRequestDispatcher("settings/upload.jsp").forward(request, response);
            }
            
            request.setAttribute("title", "Settings");
        }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
